<template>
  <div class="cesium-draw">
    <div id="cesium-drawer" />
    <canvas id="canvas-a" class="canvas" width="700" height="100"></canvas>
    <canvas id="canvas-b" class="canvas" width="700" height="100"></canvas>
    <div class="mouse-postion">{{ nowPostion.wgs84 }}</div>
    <div id="menu">
      <!--      <p><button :class="{active:!!synchronizer}" @click="handleClick('synchronizer')">同步</button></p>
      <p><button :class="{active:isRepeating}" @click="handleClick('isRepeating')">重复</button></p>
      <p><button :class="{active:videoOverlay}" @click="handleClick('videoOverlay')">播放器</button></p> -->
    </div>
  </div>
</template>

<script>
import { DownOutlined, EditOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref, watch } from 'vue';
import * as Cesium from '@cesium/Source/Cesium.js';
import MousePosition from '@functional/utils/mousePosition';

export default defineComponent({
  name: 'timeDynPointCloud', // 动态点云
  components: { DownOutlined, EditOutlined },
  setup() {
    return {
      viewer: null,
      synchronizer: null,
      isRepeating: true,
      videoOverlay: true
    };
  },
  data() {
    return {
      nowPostion: {}
    };
  },
  computed: {},
  watch: {},
  mounted() {
    this.initViewer();
  },
  beforeUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  },
  methods: {
    onViewerInited(viewer) {
      let vm = this;
      new MousePosition(viewer, pos => {
        vm.nowPostion = pos;
      });
    },

    initViewer() {
      let vm = this;

      // let osm = new Cesium.OpenStreetMapImageryProvider({
      //   url: 'https://a.tile.openstreetmap.org/'
      // });
      let osm = new Cesium.ArcGisMapServerImageryProvider({
        // url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer',
        url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
      });
      var viewer = new Cesium.Viewer('cesium-drawer', {
        imageryProvider: osm,
        contextOptions: {
          webgl: {
            alpha: true
          }
        },
        animation: false,
        // baseLayerPicker: false,
        fullscreenButton: false,
        vrButton: false,
        homeButton: false,
        timeline: true,
        selectionIndicator: false,
        infoBox: false,
        // 注释时相当于使用默认地形，解开注释相当于使用全球地形
        // terrainProvider,
        shouldAnimate: true
      });

      //开启深度检测
      viewer.scene.globe.depthTestAgainstTerrain = true;

      this.viewer = viewer;
      this.onViewerInited(viewer);

      this.initEffects();

      // this.viewer.flyTo(this.viewer.entities);
    },
    initEffects() {
      let vm = this;
      const viewer = this.viewer;

      var dates = ['2018-07-19T15:18:00Z', '2018-07-19T15:18:00.5Z', '2018-07-19T15:18:01Z', '2018-07-19T15:18:01.5Z', '2018-07-19T15:18:02Z', '2018-07-19T15:18:02.5Z'];

      var uris = [
        '//zzgis.com/cdn/3d/SampleData/Cesium3DTiles/PointCloud/PointCloudTimeDynamic/0.pnts',
        '//zzgis.com/cdn/3d/SampleData/Cesium3DTiles/PointCloud/PointCloudTimeDynamic/1.pnts',
        '//zzgis.com/cdn/3d/SampleData/Cesium3DTiles/PointCloud/PointCloudTimeDynamic/2.pnts',
        '//zzgis.com/cdn/3d/SampleData/Cesium3DTiles/PointCloud/PointCloudTimeDynamic/3.pnts',
        '//zzgis.com/cdn/3d/SampleData/Cesium3DTiles/PointCloud/PointCloudTimeDynamic/4.pnts'
      ];

      function dataCallback(interval, index) {
        return {
          uri: uris[index]
        };
      }

      var timeIntervalCollection = Cesium.TimeIntervalCollection.fromIso8601DateArray({
        iso8601Dates: dates,
        dataCallback: dataCallback
      });

      var modelMatrix = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(1.0, 1.0, 1.0));

      var pointCloud = new Cesium.TimeDynamicPointCloud({
        intervals: timeIntervalCollection,
        clock: viewer.clock,
        modelMatrix,
        style: new Cesium.Cesium3DTileStyle({
          pointSize: 5,
          // color: 'color("blue")',
        })
      });
      viewer.scene.primitives.add(pointCloud);

      var start = Cesium.JulianDate.fromIso8601(dates[0]);
      var stop = Cesium.JulianDate.fromIso8601(dates[dates.length - 1]);

      viewer.timeline.zoomTo(start, stop);

      var clock = viewer.clock;
      clock.startTime = start;
      clock.currentTime = start;
      clock.stopTime = stop;
      clock.clockRange = Cesium.ClockRange.LOOP_STOP;

      const offset = new Cesium.HeadingPitchRange(0.5, -0.5, 50.0);
      // viewer.zoomTo(pointCloud, offset);
      viewer.flyTo(pointCloud, {offset});
    },
    handleClick(value) {
      const viewer = this.viewer;
      switch (value) {
        case 'isRepeating':
          break;
      }
    }
  }
});
</script>

<style scoped lang="less">
.cesium-draw {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: white;
  font-size: 14px;

  #cesium-drawer {
    width: 100%;
    height: 100%;
    overflow: hidden;
    :deep(.cesium-viewer-bottom) {
      display: none !important;
    }
  }

  .mouse-postion {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.8;
  }
}

.chart-container {
  position: absolute;
  bottom: 7px;
  left: 7px;
  background-color: rgba(0, 0, 0, 0.5);

  #profileChart {
    width: 600px;
    height: 300px;
  }
}
#canvas-a {
  top: 10px;
  display: none;
}

#canvas-b {
  top: 120px;
  display: none;
}
#trailer {
  position: absolute;
  bottom: 75px;
  right: 0;
  width: 320px;
  height: 180px;
}
#menu {
  position: absolute;
  top: 80px;
  left: 10px;
  z-index: 999;

  button {
    background-color: rgba(0, 0, 0, 0.5);
    border-color: rgba(0, 0, 0, 0);
    cursor: pointer;

    transition: all 500ms;

    &.active {
      background-color: rgba(100, 0, 100, 0.75);
    }

    &:hover {
      opacity: 0.75;
    }
  }
}
</style>
